const express = require("express")
const pool = require("../pool")
const router = express.Router()
module.exports = router
// 后台管理
// 管理员登录
router.post("/login", (req, res, next) => {
  const {sname, spwd} = req.body
  let code = 400
  // 非空验证
  for (let key in req.body) {
    code++
    if (!req.body[key]) {
      res.send({code, message: `${key} invalid`})
      return
    }
  }
  pool.query("SELECT sid FROM Administror WHERE user=? && password=md5(?)",[sname, spwd],(err, result) => {
      if (err) {
        next(err)
        return
      }
      if (result.length != 0) {
        res.send({code: 200, message: "login success"})
      } else {
        res.send({code: 400, message: "sname or spwd error!"})
      }
    }
  )
})
// 用户列表
router.get("/userlist", (req, res, next) => {
  let {pno, pagenum} = req.query
  pno=pno ? pno : 1
  pagenum=pagenum ? pagenum:10
  let start = (pno - 1) * (pagenum * 1)
  pool.query(
    "SELECT sid,sname,user_name,sex,phone,email,detail FROM su_user LIMIT ?,?",[start, pagenum * 1],(err, result) => {
      if (err) {
        next(err)
        return
      }
      res.send({code: 200, message: "操作成功", data: result})
    }
  )
})
